package ch.sfug.data {

	/**
	 * a simple iterator, that steps over all elements of an array
	 * @author loop
	 */
	public class Iterator {

		protected var p:Number;
		protected var d:Array;

		public function Iterator( a:Array = null ) {
			reset();
			d = ( a == null ) ? [] : a;
		}

		
		/**
		 * returns true if the iterator has a next element
		 */
		public function hasNext(  ):Boolean {
			if( isNaN(p) ) {
				// start case
				return d.length > 0;
			} else {
				return p < d.length - 1;
			}
		}

		
		
		
		
		/**
		 * returns the next element
		 * if there is no next element it will return null.
		 */
		public function next(  ):Object {
			if( hasNext() ) {
				p = ( isNaN(p) ) ? 0 : p + 1;
				return d[ p ];
			} else {
				return null;
			}
		}

		
		
		
		/**
		 * returns the current active object
		 */
		public function current(  ):Object {
			return ( d != null && d.length > 0 ) ? d[ p ] : null;
		}

		
		/**
		 * resets the iterator
		 */
		public function reset(  ):void {
			p = NaN;
		}

		
		/**
		 * gets a copy of the array that the iterator is iterating over
		 */
		public function toArray(  ):Array {
			return d.slice();
		}

		
		/**
		 * sets the array to iterate over
		 */
		public function addArray( a:Array ):void {
			this.d = d.concat( a );
		}
	}
}
